package org.zfes.snowier.cloud.zbss.layer.tc.biz.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zfes.snowier.core.consts.SnowierWebConst;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.util.ZWebUtil;
import org.zfes.snowier.core.util.encypt.MD5Util;
import org.zfes.snowier.web.controller.BaseController;

import com.google.common.collect.Lists;

import org.zfes.snowier.cloud.zbss.layer.tc.biz.auth.SessionUser;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.consts.BizConst;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.model.LAuthUser;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.service.ILAuthUserService;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.vo.MenuView2;
@Controller
@RequestMapping("/auth")
public class AppController  extends BaseController  {

	@Autowired
	private ILAuthUserService authUserService;
	
	
    @RequestMapping(value="/doLogin", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object doLogin(HttpServletRequest httpRequest,String account,String password,String captcha){
    	ZAssert.hasText(account, "用户名不能为空");
		ZAssert.hasText(password, "密码不能为空");
		LAuthUser user=authUserService.selectByAccount(account);
		if(user==null){
			if(ZWebUtil.isAjax(httpRequest)){
				return ajaxDone(SnowierWebConst.SC.SC_401, "用户名或密码错误");
			}
		}
		String salt=user.getSalt();
		String pwd=user.getPassword();
		if(!MD5Util.encrypt(password, salt).equals(pwd)){
			if(ZWebUtil.isAjax(httpRequest)){
				return ajaxDone(SnowierWebConst.SC.SC_401, "用户名或密码错误");
			}
		}
		if(!Integer.valueOf("1").equals(user.getEnabled())) {
			return ajaxDone(SnowierWebConst.SC.SC_401, "用户已禁用");
		}
		httpRequest.getSession().setAttribute(BizConst.SESSION_USER, new SessionUser( user.getAccount(),  user.getDefaultRole(),  user.getUserName(),  user.getId()));
		

		if(ZWebUtil.isAjax(httpRequest)){
			return ajaxDone(SnowierWebConst.SC.SC_200, "");
		}
		return ajaxDoneSuccess("登陆成功 ");
    }
    
    
    @RequestMapping(value="/getUserInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object getUserInfo(HttpServletRequest httpRequest){
    	SessionUser su= (SessionUser) httpRequest.getSession().getAttribute(BizConst.SESSION_USER);
    	if(su==null) {
    		ZAlert.serviceLogicalError("登录超时");
    	}
    	
		return ajaxQuery("sessionUser",su);
    }
    @RequestMapping(value="/laodMenu", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public  @ResponseBody Object laodMenu(HttpServletRequest httpRequest){
    	SessionUser su= (SessionUser) httpRequest.getSession().getAttribute(BizConst.SESSION_USER);
    	if(su==null) {
    		ZAlert.serviceLogicalError("登录超时");
    	}
    	String role=su.getRole();
    	List<MenuView2> menus=Lists.newArrayList();
    	if(BizConst.ROLE.R_ADMIN.equals(role)) {
        	menus.add(new MenuView2("提成管理",  "view/tcms/percentage/percentage.html",  "bar-chart-o "));
    		menus.add(new MenuView2("人员管理",  "view/tcms/practitioner/practitioner.html",  "table"));
    		menus.add(new MenuView2("部门管理",  "view/tcms/orgnization/orgnization.html",  "map"));
    		menus.add(new MenuView2("系统管理员",  "view/tcms/lauthuser/lauthuser.html",  "gears"));
    	}else {
    		menus.add(new MenuView2("提成管理",  "view/tcms/percentage/percentage.html",  "bar-chart-o "));
    	}

     	return ajaxQuery("navMenuData", menus);
    }
}
